package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.domain.SysSuppliergoodsimplower;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.tool.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBidinfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 竞价信息Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/bidinfo")
public class SysBidinfoController extends BaseController
{
    private String prefix = "system/bidinfo";
    @Autowired
    private RuoYiConfig ruoYiConfig;
    @Autowired
    private ISysBidinfoService sysBidinfoService;
    @Autowired
    private ISysGoodsService sysGoodsService;
    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysSuppliergoodsimplowerService sysSuppliergoodsimplowerService;
    @Autowired
    private ISysUserService sysUserService;


    @RequiresPermissions("system:bidinfo:view")
    @GetMapping()
    public String bidinfo(ModelMap mmap)
    {
        SysUser sysUser=ShiroUtils.getSysUser();
        mmap.put("user",sysUser);
        return prefix + "/bidinfo";
    }

    /**
     * 查询竞价信息列表
     */
    @RequiresPermissions("system:bidinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBidinfo sysBidinfo)
    {
        Long unitid=ShiroUtils.getUserId();
        SysUser sysUser=sysUserService.selectUserById(unitid);
        if(sysUser!=null&&sysUser.getUserType().equals("11")){
           sysBidinfo.setSupplierId(sysUser.getCompanyId());
            sysBidinfo.setBidStatus("1");
        }
        startPage();
        List<SysBidinfo> list = sysBidinfoService.selectSysBidinfoList(sysBidinfo);
        return getDataTable(list);
    }
    /**
     * 查询竞价历史
     */
    @RequiresPermissions("system:bidinfo:list2")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(SysBidinfo sysBidinfo) {
        Long unitid = ShiroUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(unitid);
        if (sysUser != null && sysUser.getUserType().equals("11")) {
            /*sysBidinfo.setSupplierId(sysUser.getCompanyId());*/
            sysBidinfo.setBidStatus("1");
        }
        startPage();
        List<SysBidinfo> list = sysBidinfoService.selectSysBidinfoList(sysBidinfo);
        return getDataTable(list);
    }

        /**
         * 供应商查询竞价信息列表
         */
    @RequiresPermissions("system:bidinfo:list")
    @PostMapping("/gyslist")
    @ResponseBody
    public TableDataInfo gyslist(SysBidinfo sysBidinfo)
    {
        Long unitid=ShiroUtils.getUserId();
        sysBidinfo.setSupplierId(unitid);
        sysBidinfo.setBidStatus("1");
        startPage();
        List<SysBidinfo> list = sysBidinfoService.selectSysBidinfoList(sysBidinfo);
        return getDataTable(list);
    }
    /**
     * 导出竞价信息列表
     */
    @RequiresPermissions("system:bidinfo:export")
    @Log(title = "竞价信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysBidinfo sysBidinfo)
    {
        List<SysBidinfo> list = sysBidinfoService.selectSysBidinfoList(sysBidinfo);
        ExcelUtil<SysBidinfo> util = new ExcelUtil<SysBidinfo>(SysBidinfo.class);
        return util.exportExcel(list, "bidinfo");
    }

    /**
     * 新增竞价信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存竞价信息
     */
    @RequiresPermissions("system:bidinfo:add")
    @Log(title = "竞价信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysBidinfo sysBidinfo, int hour,Long goodsId)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Date NowDate = new Date();
        sysBidinfo.setBeginTime(NowDate);
        NowDate = new Date(NowDate.getTime()+hour*60*60*1000);
        sysBidinfo.setEndTime(NowDate);
        sysBidinfo.setBidStatus("1");//开启竞价
        String endDate=DateUtils.dateTime1(NowDate);
        SysGoods goods =  sysGoodsService.selectSysGoodsById(goodsId);
        String username = ruoYiConfig.getAccout();
        System.out.println("username="+username);
        String password = ruoYiConfig.getPassword();
        System.out.println("password="+password);
        String httpUrl = ruoYiConfig.getMsgUrl();
        System.out.println("httpUrl="+httpUrl);
        String content = "【兰州安宁财政局】"+sysUser.getUserName()+"欲采购"+goods.getGoodsName()+",请您于"+endDate+"之前登录系统进行报价！";
        SysSuppliergoodsimplower suppliergoodsimplower = new SysSuppliergoodsimplower();
        suppliergoodsimplower.setGoodsId(goodsId);
        sysBidinfo.setGoodsId(goodsId);
       /* int row=sysBidinfoService.insertSysBidinfo(sysBidinfo);*/
        sysBidinfo =sysBidinfoService.save(sysBidinfo,goodsId);
        List<SysSuppliergoodsimplower> sysSuppliergoodsimplowers = sysSuppliergoodsimplowerService.selectSysSuppliergoodsimplowerList(suppliergoodsimplower);
       /*  String telephones = "";
       for ( int i=0;i< sysSuppliergoodsimplowers.size();i++ ){
            SysSupplier sysSupplier = sysSupplierService.selectSysSupplierById(sysSuppliergoodsimplowers.get(i).getSupplierId());


            if(i==sysSuppliergoodsimplowers.size()-1){
                telephones+=sysSupplier.getTelephone();
            }else{
                telephones+=sysSupplier.getTelephone()+",";
            }

        }*/
        String[] mobile = sysBidinfo.getAttribute3().split(",");
        String telphone = sysBidinfo.getAttribute3();
        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(username).append("&");
        httpArg.append("p=").append(Message.md5(password)).append("&");
        httpArg.append("m=").append(telphone).append("&");
        String s = Message.encodeUrlString(content, "UTF-8");
        httpArg.append("c=").append(Message.encodeUrlString(content, "UTF-8"));
        String result = Message.request(httpUrl, httpArg.toString());

        if(result.equals("0")){
            return AjaxResult.success(mobile.length+"个供应商短信发送成功！");
        }else{
            return AjaxResult.error("网络异常，发送短信失败，开启竞价失败！");
        }
    }

    /**
     * 修改竞价信息
     */
    @GetMapping("/edit/{bidInfoId}")
    public String edit(@PathVariable("bidInfoId") Long bidInfoId, ModelMap mmap)
    {
        SysBidinfo sysBidinfo = sysBidinfoService.selectSysBidinfoById(bidInfoId);
        mmap.put("sysBidinfo", sysBidinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存竞价信息
     */
    @RequiresPermissions("system:bidinfo:edit")
    @Log(title = "竞价信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysBidinfo sysBidinfo)
    {
        return toAjax(sysBidinfoService.updateSysBidinfo(sysBidinfo));
    }

    /**
     * 删除竞价信息
     */
    @RequiresPermissions("system:bidinfo:remove")
    @Log(title = "竞价信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysBidinfoService.deleteSysBidinfoByIds(ids));
    }

}
